package cn.ctodb.divination.service;

import cn.ctodb.divination.service.dto.WuGe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.inject.Inject;

/**
 * Created by cc on 2017/2/5.
 */
@Service
public class WuGeService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    private BihuaService bihuaService;

    public WuGe js(String xing, String ming) {
        int tiange = 0, dige = 0, renge = 0, waige = 0, zongge = 0;
        WuGe wuGe = new WuGe();

        int renge1 = 0, renge2 = 0;
        // 姓
        char xing1, xing2;
        int bihua1 = 0, bihua2 = 0;
        xing1 = xing.charAt(0);
        bihua1 = bihuaService.getNum(xing1);
        tiange = bihua1 + 1;
        renge1 = bihua1;
        if (xing.length() > 1) {
            xing2 = xing.charAt(1);
            bihua2 = bihuaService.getNum(xing2);
            tiange = bihua1 + bihua2;
            renge1 = bihua2;
        }

        // 名----
        char ming1, ming2;
        int bihua3 = 0, bihua4 = 0;
        ming1 = ming.charAt(0);
        bihua3 = bihuaService.getNum(ming1);
        dige = bihua3 + 1;
        renge2 = bihua3;

        if (ming.length() > 1) {
            ming2 = ming.charAt(1);
            bihua4 = bihuaService.getNum(ming2);
            dige = bihua3 + bihua4;
        }
        zongge = bihua1 + bihua2 + bihua3 + bihua4;

        //  计算三才
        renge = renge1 + renge2;
        waige = zongge - renge;
        if (xing.length() == 1) {
            waige = waige + 1;
        }
        if (ming.length() == 1) {
            waige = waige + 1;
        }
        wuGe.setXing(xing);
        wuGe.setMing(ming);
        wuGe.setTiange(tiange);
        wuGe.setDige(dige);
        wuGe.setRenge(renge);
        wuGe.setWaige(waige);
        wuGe.setZongge(zongge);

        logger.debug("天格[{}];人格[{}];地格[{}];总格[{}];外格[{}];", tiange, renge, dige, zongge, waige);

        return wuGe;
    }

}
